﻿Namespace Control
    Public Class Cliente
        Implements IControl(Of Model.Pessoa)
        Implements IDisposable

        Public Function Excluir(ByVal entity As Model.Pessoa) As Boolean Implements IControl(Of Model.Pessoa).Excluir
            Dim Result As Boolean = True

            Try
                entity.Delete()
            Catch ex As Exception
                Dialog.Alert(ex.Message)
                Result = False
            End Try

            Return Result
        End Function

        Public Function ListaTudo() As System.Collections.Generic.IList(Of Model.Pessoa) Implements IControl(Of Model.Pessoa).ListaTudo
            'Dim pes As New Model.Pessoa
            'Return pes.GetByTipoCliente(Tipo.)
            Dim pes As New Model.Pessoa

            Return pes.GetByTipo(Model.Pessoa.PessoaTipo.Cliente)
        End Function

        Public Function ListaUm(ByVal entity As Model.Pessoa) As Model.Pessoa Implements IControl(Of Model.Pessoa).ListaUm
            Return Model.Pessoa.Find(entity.cd_pessoa)
        End Function

        Public Function Salvar(ByVal entity As Model.Pessoa) As Boolean Implements IControl(Of Model.Pessoa).Salvar
            Try
                entity.pessoa_cliente = Model.Pessoa.PessoaTipoStatus.Definido 'Model.Pessoa.PessoaTipo.Cliente

                ' Edson Tessaro - 15/01/2013
                ' entity._sessionScope.Flush()
                ' entity.Save()

                entity.SaveAndFlush()
            Catch ex As Exception
                Dialog.Alert(ex.Message)
            End Try

            Return True
            'Dim pes As New Model.Pessoa
            'Return pes.Salvar(_Pessoa)
        End Function


        Public Function ListaTudoPesquisa() As IList(Of Model.PessoaPesquisa)
            'Dim pes As New Model.Pessoa
            'Return pes.GetByTipoCliente(Tipo.)
            Dim pes As New Model.PessoaPesquisa

            Return pes.GetByTipo(Model.PessoaPesquisa.PessoaTipo.Cliente)
        End Function

        Public Function ListaByNomeCPFCNPJ(Optional ByVal Nome As String = "", Optional ByVal CPFCNPJ As String = "") As IList(Of Model.PessoaPesquisa)
            Dim pes As New Model.PessoaPesquisa

            If Not String.IsNullOrEmpty(Nome) Then
                Return pes.GetByNome(Model.PessoaPesquisa.PessoaTipo.Cliente, Nome)
            ElseIf Not String.IsNullOrEmpty(CPFCNPJ) Then
                Return pes.GetByCPFCNPJ(Model.PessoaPesquisa.PessoaTipo.Cliente, CPFCNPJ)
            Else
                Return ListaTudoPesquisa()
            End If

        End Function

        Public Function ExcluirPesquisa(ByVal entity As Model.PessoaPesquisa) As Boolean
            Dim Result As Boolean = True

            Try
                entity.Delete()
            Catch ex As Exception
                Dialog.Alert(ex.Message)
                Result = False
            End Try

            Return Result
        End Function

        Public Function GetById(ByVal id As Integer) As Model.Pessoa
            Return Model.Pessoa.Find(id)
        End Function

        Private disposedValue As Boolean = False        ' To detect redundant calls

        ' IDisposable
        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: free other state (managed objects).
                End If

                ' TODO: free your own state (unmanaged objects).
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
        End Sub

#Region " IDisposable Support "
        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class
End Namespace